# author:欧阳亨杰
# datetime:2022/4/29 0:40
"""
    个人界面测试
"""

import unittest

import ddt
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains
import pandas as pd


def readFile():
    file = pd.read_csv('../data/subject.csv')
    df = pd.DataFrame(file).fillna('')
    data = df.to_dict(orient='records')
    return data


@ddt.ddt
class mime_test(unittest.TestCase):
    def setUp(self) -> None:
        option = webdriver.ChromeOptions()
        option.binary_location = r'E:\Program Files\Google\Chrome\Application\chrome.exe'
        self.driver = webdriver.Chrome(options=option)
        self.url = 'http://localhost:8082/log-reg'
        self.driver.implicitly_wait(10)
        self.driver.get(self.url)
        self.driver.maximize_window()
        self.login()

    def login(self):
        username = self.driver.find_element(By.CSS_SELECTOR, '#form2 > span:nth-child(2) > input')
        password = self.driver.find_element(By.CSS_SELECTOR, '#form2 > span:nth-child(3) > input')
        username.send_keys('13975580513')
        password.send_keys('oyhj011019')
        btn = self.driver.find_element(By.CSS_SELECTOR, '#form2 > button')
        btn.click()
        time.sleep(2)
        canvas = self.driver.find_element(By.CSS_SELECTOR, 'body > canvas:nth-child(1)')
        ActionChains(self.driver).move_to_element(canvas).move_by_offset(-475, -200).pause(2).click().perform()
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR,
                                 '.loginContainer > div:nth-child(1) > span:nth-child(3) > img').click()
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, '#myList > ul:nth-child(1) > li:nth-child(3)').click()
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, '.mine-nav > ul > li:nth-child(3) > a').click()

    @ddt.data(*readFile())
    def test_add_subject(self, data):
        self.driver.find_element(By.CSS_SELECTOR, '.addColumn').click()
        subject1 = self.driver.find_elements(By.CSS_SELECTOR, '.column')
        text = self.driver.find_element(By.CSS_SELECTOR, '.addColumnNow > input')
        btn = self.driver.find_element(By.CSS_SELECTOR, '.addColumnInfo > div:nth-child(1) > span:nth-child(2)')
        text.send_keys(data.get('subject'))
        btn.click()
        time.sleep(3)
        subject2 = self.driver.find_elements(By.CSS_SELECTOR, '.column')
        if data.get('state') == 1:
            self.assertEqual(len(subject1) + 1, len(subject2))
        else:
            self.assertEqual(len(subject1), len(subject2))
        print(data)


if __name__ == '__main__':
    unittest.main()
